<?php

namespace app\attachment\controller;

use \OSS\OssClient;

class Admin extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * base64上传图片
     * 地址： attachment/Admin/baseImage
     * @return array
     */
    public function baseImage($admin_id, $config_array)
    {
        if (!isset($config_array['imgString']) || empty($config_array['imgString'])) {
            return ['state' => '图片不能为空'];
        }
        $number = preg_match('/^(data:\s*image\/(\w+);base64,)/', $config_array['imgString'], $result);
        if ($number == 0) {
            return ['state' => '非法格式'];
        }
        //图片格式
        $ext = $result[2];
        //文件内容
        $file_content = file_get_contents($config_array['imgString']);
        //文件路径名
        $path = $this->getUploadPath($admin_id) . date('Ymd', time());
        $file = $path . '/' . md5(time() . random(10, 'letter', 1)) . '.' . $ext;
        $filename = str_replace(SITE_PATH, '', $file);
        //判断上传方式
        $rs = ['state' => '上传方式暂不支持'];
        switch ($config_array['oss_type']) {
            //本地
            case 0:
                if (!is_dir($path)) {
                    @mkdir($path, 0755, true);
                }
                $rs = file_put_contents($filename, $file_content);
                $rs = ['state' => 1, 'url' => "/".$filename];
                if(!$rs){
                    $rs = ['state' => '文件写入本地失败'];
                }
                $rs['type'] = 0;
                break;
            //阿里云
            case 1:
                $rs = $this->aliStrImage($config_array, $filename, $file_content);
                if (!isset($rs['oss-request-url']) || empty($rs['oss-request-url'])) {
                    $rs = ['state' => '文件上传到阿里云失败'];
                }
                $rs = ['state' => 1, 'url' => $filename];
                $rs['url'] = $config_array['oss_domain'] . "/" . $rs['url'];
                $rs['type'] = 1;
                break;
            //七牛
            case 2:
                $rs = $this->qiniuStrImage($config_array, $filename, $file_content);
                if (!isset($rs['oss-request-url']) || empty($rs['oss-request-url'])) {
                    $rs = ['state' => '文件上传到七牛云失败'];
                }
                $rs = ['state' => 1, 'url' => $filename];
                $rs['url'] = $config_array['qiniu_domain'] . "/" . $rs['url'];
                $rs['type'] = 2;
                break;
        }
        return $rs;
    }

    /*
     * 阿里OSS字符串上传
     * */
    public function aliStrImage($config_array, $filename, $file_content)
    {
        //校验参数
        if (!isset($config_array['oss_access_key'], $config_array['oss_secret_key'], $config_array['oss_endpoint'])) {
            return ['state' => '缺少配置信息'];
        }
        if (!isset($filename, $file_content)) {
            return ['state' => '缺少文件信息'];
        }
        //实例化OSS对象
        $ossClient = new OssClient($config_array['oss_access_key'], $config_array['oss_secret_key'], $config_array['oss_endpoint']);
        $rs = $ossClient->putObject($config_array['oss_bucket'], $filename, $file_content);
        return $rs;
    }

    /*
     * 七牛OSS字符串上传
     * */
    public function qiniuStrImage($config_array, $filename, $file_content)
    {
        //校验参数
        if (!isset($config_array['qiniu_access_key'], $config_array['qiniu_secret_key'], $config_array['qiniu_bucket'])) {
            return ['state' => '缺少配置信息'];
        }
        if (!isset($filename, $file_content)) {
            return ['state' => '缺少文件信息'];
        }
        $client = new \Qiniu\Auth($config_array['qiniu_access_key'], $config_array['qiniu_secret_key']);
        $token = $client->uploadToken($config_array['qiniu_bucket']);
        $uploadMgr = new \Qiniu\Storage\UploadManager();
        $rs = $uploadMgr->put($token, $filename, $file_content);
        //实例化OSS对象
        return $rs;
    }
}